import { _decorator, CCInteger, Component, EventTouch, RichText, SpriteAtlas } from 'cc';
import { Tool } from './Tool';
import { AudioManager } from '../Basic/Manager/AudioManager';
import { SoundCode } from '../Basic/Config/BaseEnum';
// import { SymbolDetailsModel } from '../Game/Main/SymbolDetails/SymbolDetailsModel';
import { DataMgr } from '../Basic/Manager/DataManager';
import { OpenLoadModel_Ins } from '../Basic/UI/OpenLoadPanel';
import { StringUtil } from './StringUtil';
import { EventManager } from '../Basic/Manager/EventManager';
import { GameEventType } from '../Game/Config/GameEnum';
// import Tool from '../Util/Tool';
const { ccclass, property } = _decorator;
const TAG = 'RichTextClick';
@ccclass('RichTextClick')
export class RichTextClick extends Component {
  @property([CCInteger])
  iconHeight: number[] = [];
  @property(SpriteAtlas)
  atlas: SpriteAtlas;
  /**
   * 显示时
   * @example <img src='ic_w1_symbol_1307' height=${this.iconHeight[0]} />
   * @example <img src='ic_w1_symbol_1311' height=${this.iconHeight[1]} />
   * @example <img src='ic_w1_symbol_1315' height=${this.iconHeight[2]} />
   */
  protected onEnable(): void {
    const rich = this.node.getComponent(RichText);
    const str = rich.string.toString();
    // eslint-disable-next-line @typescript-eslint/no-this-alias
    const self = this;
    let index = 0;
    const retString = str.replace(/\$\{(.*?)\}/g, function (a, b: string) {
      index = Number(b.match(new RegExp(/\d/)));
      return self.iconHeight[index++].toString();
    });

    // // 动态添加新图
    const paramArr = Tool.getBBCodeImg(retString, 'img', 'src');
    this.atlas &&
      paramArr.forEach((element) => {
        // 查找
        if (rich.imageAtlas.getSpriteFrame(element) === null) {
          const sp = this.atlas.getSpriteFrame(element);
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          (rich.imageAtlas as any).spriteFrames[element] = sp;
        }
      });

    rich.string = retString;
  }

  /**
   * 富文本点击事件
   * @param eventData data_id,data_type(symbol/relic)
   * @example <on click='clickIcon' param='1001,symbol'>点我</on>
   * @example <img src='图片地址' click='clickIcon' param='1001,symbol' />
   */
  clickIcon(eventTouch: EventTouch, eventData: string) {
    // console.log('点击图标', eventData);
    // console.log(eventData.substring(eventData.indexOf(`'`) + 1, eventData.indexOf(',')));
    AudioManager.Singleton.playSound(SoundCode.BtnClick);
    const s = eventData.replace(/[']/g, '');
    const arr = StringUtil.strSplitMore(s, String, ',');
    const id = Number(arr[0]);
    // const id = Number(eventData.substring(eventData.indexOf(`'`) + 1, eventData.indexOf(',')));
    // const type = eventData.substring(eventData.indexOf(',') + 1, eventData.indexOf(`'`, eventData.indexOf(',') + 1));
    OpenLoadModel_Ins.showAni = false;

    // if (SymbolDetailsModel.Ins().basePanel && SymbolDetailsModel.Ins().basePanel.node && SymbolDetailsModel.Ins().basePanel.node.parent != null) {
    //   EventManager.Ins().emit(GameEventType.SYMBOL_DETAIL_SHOW, DataMgr.tables.TbSymbol.get(id));
    // } else {
    //   SymbolDetailsModel.Ins().Show({ data: DataMgr.tables.TbSymbol.get(id) });
    // }

    // FrameManager.I.getFrame(FrameType.LogFrame)
    //   .then((data) => {
    //     if (data.parent == null) {
    //       if (type == 'symbol') {
    //         data.getComponent(LogFrame).setData(new GameSymbol(id), null, null, null, null);
    //       } else {
    //         data.getComponent(LogFrame).setData(new GameRelic(id), null, null, null, null);
    //       }
    //       FrameManager.I.showFrame(FrameType.LogFrame);
    //     } else {
    //       data.getComponent(LogFrame).logUx.onClose(() => {
    //         FrameManager.I.hideFrame(FrameType.LogFrame);
    //         if (type == 'symbol') {
    //           data.getComponent(LogFrame).setData(new GameSymbol(id), null, null, null, null);
    //         } else {
    //           data.getComponent(LogFrame).setData(new GameRelic(id), null, null, null, null);
    //         }
    //         FrameManager.I.showFrame(FrameType.LogFrame);
    //       });
    //     }
    //   })
    //   .catch((err) => {
    //     LogUtil.e(TAG, 'ShowLogFrame: ', err);
    //   });
    // LogUtil.i(TAG, 'CardInfo:', this);
  }
}
